<?php
/**
 * This file is part of the Agora distribution. 
 * Detailed copyright and licensing information can be found
 * in the gpl-3.0.txt file which should be included in the distribution.
 * 
 * @version		$Id: 3.0.14 Olympus 2010-09-28 rammstein4o $
 * @copyright  2007 - 2010 jVitals
 * @license   GPLv3 Open Source
 * @link       http://jvitals.com
 * @since      File available since initial release
 */
defined ('IN_AGORA') or die;

class ProfilePostsModel extends Model {

    function __construct() {
		parent::__construct('profileposts');
	}


	function loadAll($key = '', $per_page = null, $page = null, $user_id, $allowed_forums=array()) {
		if (is_null($per_page) || is_null($page)) {
			$limit = '';
		} else {
			$limit = Model::getLimit($per_page, $page);
		}
		if ($allowed_forums) {
			$where = ' AND t.forum_id IN ('.implode(", ", $allowed_forums).') ';
		} else {
			$where = '';
		}
		
		$where .= ' AND p.approved = 1 ';
		
		$this->db->setQuery('SELECT t.id, t.poster, t.subject, t.descrip_t, t.posted, t.last_post, t.last_post_id, t.last_poster, t.num_views, t.num_replies, 
								t.closed, t.sticky, t.moved_to, t.forum_id, t.question, t.yes, t.`no` , t.icon_topic, f.forum_name, f.forum_desc
							FROM #__agora_topics t
								INNER JOIN #__agora_posts p ON t.id = p.topic_id
								INNER JOIN #__agora_forums f ON f.id = t.forum_id 
							WHERE p.poster_id = ' . $user_id 
								. $where .' 
							GROUP BY topic_id '. $this->order .' '. $limit
		);

		return $this->db->loadAssocList($key);
	}


    function getTopicCount($user_id, $allowed_forums=array()) {
		if ($allowed_forums) {
			$where = ' AND t.forum_id IN ('.implode(", ", $allowed_forums).') ';
		} else {
			$where = '';
		}
		
		$where .= ' AND p.approved = 1 ';

		$this->db->setQuery('SELECT t.id FROM #__agora_topics t INNER JOIN #__agora_posts p ON t.id = p.topic_id WHERE p.poster_id=' . $user_id . $where . ' GROUP BY topic_id');

		$posts = $this->db->loadAssocList();

		if (!is_null($posts)){
			return count($posts);
		}else{
			return 0;
		}
	}

}

?>
